package com.hxkj.controller;

import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.FunctionMenu;
import com.hxkj.service.UserService;
import com.hxkj.token.annotation.LoginAuthor;
import com.hxkj.token.po.LoginToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户登录控制器
 * <p>
 * Create by chenwei on 2018/5/24 10:57
 */
@RestController
@Slf4j
public class AuthLoginController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录
     *
     * @param authUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public LoginToken login(@ModelAttribute AuthUser authUser,String code,String uuid) throws Exception {
        return userService.login(authUser,code,uuid);

    }


    /**
     * 菜单功能列表
     *
     * @param siteId
     * @return
     * @throws Exception
     */
    @RequestMapping(value ="/index",method = RequestMethod.POST)
    public List<FunctionMenu> index(@RequestParam(value = "siteId", required = false) String siteId) throws Exception {
        List<FunctionMenu> functions = userService.findFunctionMenuByAccount(siteId);
        return functions;
    }



    /**
     * 退出
     * @return
     */
    @RequestMapping(value ="/out",method = RequestMethod.POST)
    @ResponseBody
    public void loginOut(@LoginAuthor AuthUser userInfo) throws Exception {
        userService.loginOut(String.valueOf(userInfo.getUserId()));
    }


    /**
     * 用户信息
     * @return
     */
    @RequestMapping(value ="/user/info",method = RequestMethod.POST)
    @ResponseBody
    public AuthUser info(@LoginAuthor AuthUser userInfo) throws Exception {
        return userService.getUserInfo();
    }
}
